﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Garden.Model;
using Kaixin.BLL;
using Kaixin.BLL.Helper;
using Kaixin.BLL.Model;
using RegexHelper = Garden.Helper.RegexHelper;
using System.Threading;
using System.Collections;

namespace Garden.Func
{
    /// <summary>
    /// 农夫
    /// </summary>
    public class Farmer
    {
        private HarvestMan harvestMan = new HarvestMan();
        private WaterMan waterMan = new WaterMan();
        private GrassMan grassMan = new GrassMan();
        private VerminMan verminMan = new VerminMan();
        private PloughMan plougMan = new PloughMan();
        private SowMan sowMan = new SowMan();

        private LogWrite _log;
        private string _verify;//验证字符串
        public Farmer(LogWrite log) 
        {
            _log = log;
        }

        public void DoWork()
        {
            string myGarden = WebHelper.Get("http://www.kaixin001.com/!house/garden/index.php");
            List<GroupCollection> list = RegexHelper.Matches(myGarden, RegexHelper.VerifyReg);
            if (list.Count == 0)
            {
                return;
            }
            _verify = list[0][1].Value;
            Config config = Config.CreateConfig();//从配置中读取好友列表
            _log("好友列表是配置中读取的并非实时更新，如果你加过新的好友可以更新好友列表后继续工作");
            _log("开始工作...");
            GardenWork(new User{ UID="0"});//自家花园
            foreach (var user in config.FirendList)
            {
                GardenWork(user);
            }
            _log("工作结束");
        }

        private void GardenWork(User user)
        {
            List<GroupCollection> list;
            string url = string.Format("http://www.kaixin001.com/!house/!garden//getconf.php?verify={0}&fuid={1}&r=0.2959122322499752", _verify, user.UID);

            _log(string.Format("进入{0}的花园", (user.UID != "0" ? "好友" + user.Name : "我")));
            string gardenJSON = WebHelper.Get(url);
            list = RegexHelper.Matches(gardenJSON, RegexHelper.ItemReg);
            if (list.Count == 0)
            {
                if (RegexHelper.Match(gardenJSON, RegexHelper.NoAdd) == RegexHelper.NoAdd )
                {
                    _log(string.Format("好友{0}未添加花园组件", user.Name));
                }
                else
                {
                    _log("访问过快系统将暂停10秒后继续访问");
                    Thread.Sleep(10000);
                }
                return;
            }
            foreach (var group in list)
            {
                Farmland land = Utility.CreateFarmLand(group);
                // _log(string.Format("{0}号{1}地：果实【{3}】状态{7} --成熟度{2}、水{4}滴{5}{6}", land.FarmNum, land.IsLoveFarm ? "爱心" : string.Empty,land.Growpercent,land.Seed.name,land.Water,land.IsGrass?"、长草":string.Empty,land.IsVermin?"、长虫":string.Empty,land.Cropsstatus));                   
                land.UID = user.UID;
                harvestMan.DoWork(land, _log, _verify);
                plougMan.DoWork(land, _log, _verify);
                if (GardenConfig.AutoSow)
                {
                    sowMan.DoWork(land, _log, _verify);
                }
                if (GardenConfig.Water)
                {
                    waterMan.DoWork(land, _log, _verify);
                }
                if (GardenConfig.Grass)
                {
                    grassMan.DoWork(land, _log, _verify);
                }
                if (GardenConfig.Vermin)
                {
                    verminMan.DoWork(land, _log, _verify);
                }
            }
            Thread.Sleep(1000);
        }
    }
}
